/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.PlatformUI;

/**
 * This page allows to get the following information in order to create the new debugging property:
 *  - Property debugging enabled or disabled
 *  
 *  @author Mario Moreno
 */

public class AddPropertyWizardPage1 extends WizardPage {
	
	
	private Combo retryCombo;
	
	private Button enableDebugging;
	
	

	/**
	 * Constructor for AddGoalWizardPage1
	 * 
	 * @param pageName
	 */
	public AddPropertyWizardPage1(ISelection selection) {
		super("wizardPage");
		setTitle("Add Property Wizard");
		setDescription("Page 1: Property details");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.horizontalSpacing = 50;
		layout.verticalSpacing = 9;
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addPropertyWizard");
			
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("&Debugging (optional):");
		retryCombo = new Combo(container, SWT.READ_ONLY);
		retryCombo.setItems (new String [] {"true","false"});
		retryCombo.select(0);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		retryCombo.setLayoutData(gd);
		retryCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableDebugging = new Button (container, SWT.CHECK);
		enableDebugging.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
	
		initialize();
		dialogChanged();
		setControl(container);
	}


	private void initialize() {
		enabledPropertiesChanged();
	}

	/*
	 * Ensures that all text fields are set.
	 */
	private void dialogChanged() {
		
		updateStatus(null);
	}
	
	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged(){
		retryCombo.setEnabled(enableDebugging.getSelection());
		
		dialogChanged();
		
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	

	/**
	 * @return the debugging value or null if the control is disabled
	 */
	public String getDebugging() {
		if(enableDebugging.getSelection()){
			return retryCombo.getText();
		}
		return null;
	}

}